package com.demo.web;

import com.demo.common.Constants;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.Info;
import com.demo.service.IInfoService;
import com.demo.vo.InfoVo;
import com.demo.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/info")
public class InfoController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IInfoService dataService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> create(Info arg, HttpSession session) {
        UserVo user_ = (UserVo) session.getAttribute(Constants.USER);
        if (user_ == null) {
            return new BaseResult(false, "未登录!", 401);
        }
        arg.setAuthor(user_.getUserName());
        int service_res = dataService.insertSelective(arg);
        BaseResult<Object> res = new BaseResult(true, service_res);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Integer> delete(Info arg) {
        int service_res = dataService.deleteByPrimaryKey(arg.getId());
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);

        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(Info arg) {
        int service_res = dataService.updateByPrimaryKeySelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Info> get(Info arg) {
        Info service_res = dataService.selectByPrimaryKey(arg.getId());
        BaseResult<Info> res = new BaseResult<Info>(true, service_res);
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(InfoVo arg) {
        List<Info> list = dataService.queryByPage(arg);
        int total = dataService.queryCount(arg);
        arg.getPage();
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        System.out.println("Size:" + arg.getSize());
        System.out.println("getSortField:" + arg.getSortField());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
